<?php

/**
 * This is the model class for table "{{user}}".
 *
 * The followings are the available columns in table '{{user}}':
 * @property integer $id
 * @property string $username
 * @property string $password
 * @property string $salt
 * @property string $email
 * @property string $birthday
 * @property string $address
 * @property string $phone
 * @property integer $type
 */
class User extends CActiveRecord
{

    const STATUS_NEW = 0;
    const STATUS_SUCESS = 1;

    public $verifyCode;
    public $repassword;
    public $rememberMe;
    public $pwdChanged = false;

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return User the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return '{{user}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('username, password, email, birthday, address, phone', 'required'),
            array('type', 'numerical', 'integerOnly' => true),
            array('username', 'length', 'max' => 100),
            array('password', 'length', 'max' => 36),
            array('salt, email, address', 'length', 'max' => 200),
            array('phone', 'length', 'max' => 20),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, username, password, salt, email, birthday, address, phone, type', 'safe', 'on' => 'search'),
            array('verifyCode', 'captcha', 'allowEmpty' => !CCaptcha::checkRequirements(),
                'message' => 'Mã Xác Nhận Nhập Không Đúng'),
            array('email', 'email', 'message' => '{attribute} không hợp lệ'),
            array('repassword', 'compare', 'compareAttribute' => 'password', 'message' => "{attribute} không trùng khớp"),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    protected function saltPassword($salt)
    {
        return md5($salt);
    }

    public function setAttributes($attributes, $safe = true)
    {
        foreach ($attributes as $name => $value)
        {
            $this->setAttribute($name, $value);
        }
        return true;
    }

//    public function setAttribute($name, $value)
//    {
//        if ($name == "password")
//            $this->pwdChanged = true;
//        parent::setAttribute($name, $value);
//    }

    protected function beforeSave()
    {
        if ($this->isNewRecord)
        {
            $this->password = $this->createPassword();
        }
        elseif ($this->pwdChanged)
        {
            $this->password = $this->createPassword();
        }
        return parent::beforeSave();
    }

    public function createPassword()
    {
        $this->salt = $this->saltPassword($this->username . "salt0103938273646");
        return $this->encrypt($this->password, $this->salt);
    }

    public function encrypt($rawPwd, $salt)
    {
        return md5("{$rawPwd}.{$salt}");
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'username' => Yii::t('register', 'Username'),
            'password' => Yii::t('register', 'Password'),
            'salt' => Yii::t('register', 'Salt'),
            'email' => Yii::t('register', 'Email'),
            'birthday' => Yii::t('register', 'Birthday'),
            'address' => Yii::t('register', 'Address'),
            'phone' => Yii::t('register', 'Phone'),
            'type' => Yii::t('register', 'Type'),
            'verifyCode' => Yii::t('register', 'Verify Code'),
            'repassword' => Yii::t('register', 'Repassword'),
            'rememberMe' => yii::t('login', 'Remember Me')
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('username', $this->username, true);
        $criteria->compare('password', $this->password, true);
        $criteria->compare('salt', $this->salt, true);
        $criteria->compare('email', $this->email, true);
        $criteria->compare('birthday', $this->birthday, true);
        $criteria->compare('address', $this->address, true);
        $criteria->compare('phone', $this->phone, true);
        $criteria->compare('type', $this->type);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

}